package com.rjwh.dingdong.client.util;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class TheradPoolUtil {
	private static TheradPoolUtil poolUtil;
	private ExecutorService executor;
	private int nThreads = 3;

	private TheradPoolUtil() {

		executor = Executors.newFixedThreadPool(nThreads, new ThreadFactory() {
			@Override
			public Thread newThread(Runnable r) {
				Thread t = new Thread(r);
				// 设置线程的优先级别，让线程先后顺序执行（级别越高，抢到cpu执行的时间越多）
				t.setPriority(Thread.NORM_PRIORITY - 1);
				return t;
			}
		});
	}

	public static TheradPoolUtil getInstant() {
		if (poolUtil == null) {
			poolUtil = new TheradPoolUtil();
		}
		return poolUtil;
	}

	public ExecutorService getExecutor() {
		return executor;
	}

	/**
	 * 设置线程数量
	 * 
	 * @Title: setThreadsSize
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param @param size
	 * @return void
	 * @throws
	 * @history 1.YYYY-MM-DD author: description:
	 */
	public void setThreadsSize(int size) {
		this.nThreads = size;
		executor = Executors.newFixedThreadPool(nThreads, new ThreadFactory() {
			@Override
			public Thread newThread(Runnable r) {
				Thread t = new Thread(r);
				// 设置线程的优先级别，让线程先后顺序执行（级别越高，抢到cpu执行的时间越多）
				t.setPriority(Thread.NORM_PRIORITY - 1);
				return t;
			}
		});
	}

}
